<template>
	<view class="cart">
		<zgj-row v-for="(item,idx) of items">
			<zgj-col :span="8">
				<view class="zgj-row">{{item.oename}}</view>
			</zgj-col>
			<zgj-col :span="4">
				<view class="zgj-row">{{item.qualityName}}</view>
			</zgj-col>
			<zgj-col :span="4">
				<view class="zgj-row price">{{item.price|money}}</view>
			</zgj-col>
			<zgj-col :span="8"><uv-number-box v-model="item.num" :min="0"></uv-number-box></zgj-col>
		</zgj-row>
		<view class="zgj-row" style="margin-top:20px">
			<checkbox-group @change="invoiceChange">
				<checkbox :value="1">发票</checkbox>
			</checkbox-group>
			<view class="tip">开票会收取{{rate*100}}%的费用</view>
		</view>
		<view class="sum">
			<span>总计：</span>
			<span class="price" style="font-size: 16px;">￥{{amountDisplay}}</span>
		</view>
		<uni-section title="配送地址" type="line" style="margin-top: 5px;">
			<uni-easyinput v-model="contact" placeholder="联系人"></uni-easyinput>
			<uni-easyinput v-model="phone" placeholder="联系电话"></uni-easyinput>
			<uni-easyinput v-model="address" placeholder="配送地址"></uni-easyinput>
		</uni-section>
		<view class="zgj-row" style="margin-top: 20px;">
			<button type="primary" @click="createOrder(1)">立即支付</button>
			<button @click="createOrder(0)">生成订单</button>
		</view>
	</view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	import UvNumberBox from '../../components/uv-number-box/uv-number-box.vue'
	import ZgjRow from '../../components/zgj-row/zgj-row.vue'
	import ZgjCol from '../../components/zgj-col/zgj-col.vue'
	import Decimal from '../../common/decimal.min.js'
	var app = getApp()
	export default {
		components: {
			UvNumberBox,
			ZgjRow,
			ZgjCol
		},
		data() {
			return {
				items: app.globalData.cart.quote.partList,
				rate: 0,
				invoice: 0,
				contact: '',
				address: '',
				phone: ''
			}
		},
		computed: {
			userInfo() {
				return store.userInfo
			},
			amount() {
				let carts = this.items.filter(o => o.num && o.num > 0)
				if (carts.length === 0) return 0
				var sum = new Decimal(0)
				carts.forEach(o => {
					sum=sum.plus(o.price * o.num)
				})
				if (this.invoice === 1) {
					sum=sum.times(1 + this.rate)
				}
				console.log(sum.toNumber(),sum.toString())
				return sum.toNumber()
			},
			amountDisplay(){
				var sum = new Decimal(this.amount)
				return sum.times(0.01).toNumber().toFixed(2)
			}
		},
		async onLoad(option) {
			
			this.getPayconfig()
			var shop = app.globalData.shop
			console.log("shop",shop)
			if (!shop) {
				shop = await this.getShop()
		

			}
			if (shop) {
				app.globalData.shop = shop
				this.address = shop.address
			}
			this.phone = this.userInfo.mobile
			this.contact = this.userInfo.nickname
		},
		methods: {
			async getShop() {
				console.log('userid:',this.userInfo._id)
				let res = await uniCloud.databaseForJQL().collection("zgj-shop")
					.where('user_id=="' + this.userInfo._id + '"')
					.get()
				console.log("cloud res", res)
				if (res.errCode !== 0) {
					uni.showModal({
						title: '错误信息',
						content: '读取门店信息失败，请重试',
						showCancel: false
					})
					return null
				}
				return res.data.length > 0 ? res.data[0] : null
			},
			getPayconfig() {
				this.$http.get("/web/ai/payconfig", {}, (resp) => {
					this.rate = resp.rate
				})
			},
			invoiceChange(e) {
				this.invoice = e.detail.value.length === 1 ? 1 : 0
			},
			createOrder(pay) {
				let carts = this.items.filter(o => o.num && o.num > 0)
				if (carts.length === 0) {
					uni.showToast({
						icon: 'error',
						title: '请选择配件'
					})
					return
				}
				var items = []
				carts.forEach(o => {
					items.push({
						prod_id: o.id,
						name: o.oename,
						num: o.num,
						quality_name: o.qualityName
					})
				})
				let ask = app.globalData.cart.ask
				let quote = app.globalData.cart.quote
				var orderData = {
					brand_id: ask.brand_id,
					brand_name: ask.brand,
					context_id: ask.enquiry.id,
					channel_id: quote.channelId,
					channel_name: quote.channelName,
					shop_id: ask.shop_id,
					shop_name: ask.shop_name,
					quote_id: quote.id,
					user_id: this.userInfo._id,
					contact_phone: this.phone,
					contact: this.contact,
					address: this.address,
					invoice: this.invoice,
					vin:ask.vin,
					source: 'MY',
					items: items
				}
				this.$http.post('/web/ai/order/create', orderData, (resp) => {
					uni.redirectTo({
						url:'/uni_modules/zgj-pages/pages/orders/partorderinfo?id='+resp+'&pay='+pay
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.cart {
		padding: 10px;
		font-size: 13px;
		align-items: center !important;

		.uni-col {
			min-height: 30px;
		}

		.uni-row {
			justify-content: flex-start !important;
			min-height: 30px;
			align-items: center;
		}

		.price {
			width: 100%;
			color: $uni-color-warning;
			text-align: right;
		}

		.tip {
			margin-left: 10px;
		}

		.sum {
			width: 100%;
			text-align: right;

			span {}
		}
	}
</style>